<!-- 药品管理类 -->
<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="drugCode">
              <el-input v-model="filters.drugCode" clearable placeholder="药品编号"></el-input>
            </el-form-item>
            <el-form-item prop="queryName">
              <el-input v-model="filters.queryName" clearable placeholder="药名或别名"></el-input>
            </el-form-item>
            <el-form-item prop="state">
              <el-select v-model="filters.state" clearable placeholder="状态">
                <el-option v-for="item in state" :key="item.value" :label="item.lable" :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新 增</el-button>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table :data="dataList" :fit="true" highlight-current-row>
        <el-table-column prop="drugCode" label="药品编号" align="center" width="120"></el-table-column>
        <el-table-column prop="drugName" label="通用名" width="120" align="center"></el-table-column>
        <el-table-column prop="drugNickName" label="别名" align="center"></el-table-column>
        <!-- <el-table-column prop="drugUrl" label="图片" width="150"></el-table-column> -->
        <el-table-column label="价格" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.drugPrice }} 元 </span>
          </template>
        </el-table-column>
        <el-table-column width="120px" align="center" label="规格-单位-库存">
          <template slot-scope="scope">
            <span>{{ scope.row.drugSpecs }} - {{ scope.row.drugUnit }} - {{ scope.row.drugInventory }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="factoryName" label="药厂名称" align="center"></el-table-column>
        <el-table-column prop="factoryCode" label="药厂编码" align="center"></el-table-column>
        <el-table-column label="医保" width="100" align="center">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" :type="formatTrueType(scope.row.isInsurance)" effect="plain">
                {{ formatTrue(scope.row.isInsurance) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="基药" width="100" align="center">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" :type="formatTrueType(scope.row.isHeds)">
                {{ formatTrue(scope.row.isHeds) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="instructions" label="附带说明书" :show-overflow-tooltip="true" width="150">
        </el-table-column> -->
        <el-table-column label="状态" align="center" width="80">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" :type="formatStateType(scope.row.state)" effect="dark">
                {{ formatState(scope.row.state) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template slot-scope="scope">
            <el-button v-has="permissionSearch" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <!--  -1.删除 0.禁用 1.启用-->
                <!-- 删除逻辑：没被删除，在禁用中 | 启用逻辑：没删除，在禁用中 | 禁用逻辑：在启用中未被删除-->
                <el-dropdown-item v-has="permissionEdit" v-if="scope.row.state === 1" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
                <el-dropdown-item v-has="permissionState" v-if="scope.row.state === 1" @click.native="handleClickDisable(scope.row, 0)">禁用</el-dropdown-item>
                <el-dropdown-item v-has="permissionState" v-if="scope.row.state === 0" @click.native="handleClickSuccess(scope.row, 1)">启用</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" v-if="scope.row.state === 0" @click.native="handleIsDelete(scope.row, -1)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import * as message from '@/constants/message';
import ConsumeApi from '@/api/consume/consume';
import { getStorage } from '@/utils/auth';
import * as stn from '@/constants/storeName';

export default {
  name: 'DrugManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      drugDataList: [],
      selectIds: [],
      userInfo: {},
      userRole: '',
      // 状态：(-1.删除 0.禁用 1.启用
      state: [
        { lable: '禁用', value: 0 },
        { lable: '启用', value: 1 },
        { lable: '删除', value: -1, disabled: true },
      ],
      filters: {
        // 搜索框内容
        queryName: '',
        drugCode: '',
        state: '',
        drugNickName: '',
      },

      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },

      permissionAdd: 'SYSTEM_MANAGER_CONSUME_DRUG_LIST_ADD', //添加
      permissionSearch: 'SYSTEM_MANAGER_CONSUME_DRUG_LIST_SEARCH', // 查看
      permissionEdit: 'SYSTEM_MANAGER_CONSUME_DRUG_LIST_EDIT', // 编辑
      permissionDelete: 'SYSTEM_MANAGER_CONSUME_DRUG_LIST_DELETE', // 删除
      permissionState: 'SYSTEM_MANAGER_CONSUME_DRUG_LIST_STATE', // 药品列表状态 启用/禁用
      excelData: {},
      allList: [],
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    // if(this.$route.meta.keepAlive){
    this.initDate();
    this.getTableHeight();
    this.listDrug();
    // }
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      this.userInfo = getStorage(stn.USER_INFO);
      // 把个人信息暂时存储在网页中
      if (this.userInfo) {
        this.userRole = this.userInfo.userRole;
        // 用户状态，以控制权限
      }
      _this.initDate();
      //_this.listHospital();
      _this.getTableHeight();
      _this.listDrug();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },

    getTableHeight() {
      // 设置表格高度
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    initDate() {
      // 设置基本时间参数
      let begin = new Date();
      begin.setHours(0);
      begin.setMinutes(0);
      begin.setSeconds(0);
      begin.setMonth(begin.getMonth() - 1);
      let end = new Date();
      end.setHours(23);
      end.setMinutes(59);
      end.setSeconds(59);
      this.filters.date = [begin, end];
    },

    resetForm() {
      // 重置表单
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    formatTrue(cellValue) {
      if (cellValue === 1) {
        return '是';
      } else if (cellValue === 0) {
        return '否';
      }
    },
    formatTrueType(cellValue) {
      if (cellValue === 1) {
        return 'warning';
      } else if (cellValue === 0) {
        return 'info';
      }
    },
    // 药品状态
    formatState(cellValue) {
      if (cellValue === -1) {
        return '已被删除';
      } else if (cellValue === 0) {
        return '禁用';
      } else if (cellValue === 1) {
        return '启用';
      }
    },
    formatStateType(cellValue) {
      if (cellValue === -1) {
        return 'warning';
      } else if (cellValue === 0) {
        return 'info';
      } else if (cellValue === 1) {
        return 'success';
      }
    },

    handleClickDetail(scope) {
      // 查看药品详情，并且传一个id过去
      let _this = this;
      _this.$router.push({
        path: '/consume/drugDetail',
        query: { id: scope.drugId },
      });
    },

    // 表单状态管理
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listDrug();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listDrug();
    },
    // 分页插件

    handleClickEditor(row) {
      let _this = this;
      _this.$router.push({
        path: '/consume/drugAdd',
        query: { id: row.drugId },
      });
    },
    // 编辑

    handleClickDisable(row, state) {
      let _this = this;
      this.$confirm('是否确认禁用该药品', message.TITLE_PROMPT).then(() => {
        ConsumeApi.deleteDrug({
          drugId: row.drugId,
          state: state,
        }).then((res) => {
          _this.$Utils.notifySuccess();
          _this.listDrug();
        });
      });
    },
    handleClickSuccess(row, state) {
      let _this = this;
      this.$confirm('是否确认启用该药品', message.TITLE_PROMPT).then(() => {
        ConsumeApi.deleteDrug({
          drugId: row.drugId,
          state: state,
        }).then((res) => {
          _this.$message({
            type: 'success',
            message: '操作成功',
          });
          _this.listDrug();
        });
      });
    },
    handleIsDelete(row, state) {
      let _this = this;
      _this.$confirm('是否确认删除该药品', message.TITLE_PROMPT).then(() => {
        ConsumeApi.deleteDrug({
          drugId: row.drugId,
          state: state,
        }).then((res) => {
          _this.$message({
            type: 'success',
            message: '操作成功',
          });
          _this.listDrug();
        });
      });
    },
    // 编辑状态功能
    onSearch() {
      // 搜索按钮
      this.page.pageNo = 1;
      this.listDrug();
    },
    listDrug() {
      // 查询列表数据
      let _this = this;
      let cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        state: _this.filters.state,
        queryName: _this.filters.queryName,
        drugCode: _this.filters.drugCode,
      };
      ConsumeApi.listDrug(cond).then((res) => {
        // 定义分页内容
        _this.dataList = res.data.list;
        _this.page.pageTotal = res.data.total;
      });
    },
    handleClickAdd() {
      this.$router.push({ path: '/consume/drugAdd' });
    },
  },
};
</script>
<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
